package com.wk.springboot.hwjs;

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class TreeMain {

    public static void main(String[] args) {
        Scanner in  = new Scanner(System.in);
        int n = in.nextInt();
        Map<Integer,Integer> deepMap = new HashMap();
        Map<Integer,Integer> childMap = new HashMap();

        deepMap.put(0, 1);
        childMap.put(0, 0);
        int max = 0;
        for(int i=0; i<n ;i++){

            int parent = in.nextInt();
            int child = in.nextInt();

            //首先判断输入的节点的正确性
            if(!deepMap.containsKey(parent) && childMap.get(parent)>=2){
                continue;
            }

            //设置子节点深度
            deepMap.put(child,deepMap.get(parent)+1);
            childMap.put(parent,childMap.get(parent)+1);
            childMap.put(child,0);
            max = Math.max(max,deepMap.get(child));
        }
        System.out.println(max);
    }
}
